#pragma once

#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>

// 工具接口类

class Util
{
public:
    static ssize_t ReadLine(int sock, std::string &out)
    {
        char ch = '\0';
        while(ch != '\n')
        {
            ssize_t s = recv(sock,&ch,1,0);
            if(s > 0)
            {
                if(ch == '\r')
                {
                    // 往后读一位字母
                    recv(sock,&ch,1,MSG_PEEK);
                    if(ch == '\n')
                    {
                        // \r\n -> \n
                        recv(sock,&ch,1,0);
                    }
                    else
                    {
                        // \r -> \n
                        ch = '\n';
                    }
                }
            }
            else if(s == 0)
            {
                return 0;
            }
            else
            {
                return -1;
            }
            out.push_back(ch);
        }
        return out.size();
    }
};